//
//  HDOrderListViewController.m
//  HDecoration
//
//  Created by 曹群 on 15/8/16.
//  Copyright (c) 2015年 HDecoration. All rights reserved.
//

#import "HDOrderListTableViewController.h"

@interface HDOrderListTableViewController ()

@end

@implementation HDOrderListTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identify = @"cell";
    HDOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    if (cell==nil) {
        cell = [[HDOrderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.orderType = self.orderType;
    cell.customerInfo = self.dataArray[indexPath.section];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [HDOrderCell cellHeight:self.dataArray[indexPath.row]];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        return 0.01;
    }
    return 7;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section==self.dataArray.count-1) {
        return 10;
    }
    return 0.01;
}

@end

@interface HDOrderCell ()
@property(nonatomic,strong)DOImageView *photo;
@property(nonatomic,strong)UILabel *nameLabel;
@property(nonatomic,strong)UILabel *stationTimeLabel;
@property(nonatomic,strong)UILabel *typeLabel;
@property(nonatomic,strong)UILabel *learnTimeLabel;
@property(nonatomic,strong)UILabel *remarkLabel;
@property(nonatomic,strong)GXButton *needCompanyBtn;
@property(nonatomic,strong)UILabel *priceLabel;
@property(nonatomic,strong)GXButton *callBtn;
@property(nonatomic,strong)UILabel *orderStatusLabel;
@property(nonatomic,strong)UILabel *subjectLabel;
@property(nonatomic,strong)UILabel *statusTitleLabel;
@property(nonatomic,strong)UIImageView *line;
@end

@implementation HDOrderCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self.contentView addSubview:self.photo];
        [self.contentView addSubview:self.nameLabel];
        [self.contentView addSubview:self.stationTimeLabel];
        [self.contentView addSubview:self.typeLabel];
        [self.contentView addSubview:self.subjectLabel];
        UIImageView *line1 = [[UIImageView alloc] initWithFrame:CGRectMake(self.callBtn.left-5, 20, 0.5, 45)];
        line1.image = [UIImage imageNamed:@"dot"];
        [self.contentView addSubview:line1];
        [self.contentView addSubview:self.callBtn];
        UIImageView *line2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, self.photo.bottom+15, APPFRAME.size.width, 0.5)];
        line2.image = [UIImage imageNamed:@"dot"];
        [self.contentView addSubview:line2];
        [self.contentView addSubview:self.learnTimeLabel];
        [self.contentView addSubview:self.remarkLabel];
        [self.contentView addSubview:self.priceLabel];
        [self.contentView addSubview:self.needCompanyBtn];
        self.line = [[UIImageView alloc] initWithFrame:CGRectMake(0, self.remarkLabel.bottom+15, APPFRAME.size.width, 0.5)];
        self.line.image = [UIImage imageNamed:@"dot"];
        [self.contentView addSubview:self.line];
        self.statusTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, self.remarkLabel.bottom+15, 100, 44)];
        self.statusTitleLabel.font = [UIFont systemFontOfSize:13];
        self.statusTitleLabel.textColor = kDarkGrayColor;
        self.statusTitleLabel.text = @"订单状态";
        [self.contentView addSubview:self.statusTitleLabel];
        [self.contentView addSubview:self.orderStatusLabel];
    }
    return self;
}

+ (CGFloat)cellHeight:(HDCustomerInfo *)customerInfo
{
    if (customerInfo.orderType==HDOrderTypePractice) {
        CGFloat remarkLabelWidth;
        if (customerInfo.needCompany) {
            remarkLabelWidth = APPFRAME.size.width-160;
        } else {
            remarkLabelWidth = APPFRAME.size.width-90;
        }
        CGSize size = [HDUtil sizeWithString:[NSString stringWithFormat:@"备注：%@",customerInfo.remark] font:[UIFont systemFontOfSize:13] size:CGSizeMake(remarkLabelWidth, CGFLOAT_MAX)];
        return size.height>15?size.height+180:195;
    }
    return 195;
}

- (void)setCustomerInfo:(HDCustomerInfo *)customerInfo
{
    _customerInfo = [customerInfo copy];
    self.photo.imgURLStr = _customerInfo.photoUrl;
    self.nameLabel.text = _customerInfo.name;
    self.stationTimeLabel.text = [NSString stringWithFormat:@"平台学时：%@",_customerInfo.stationTime];
    self.typeLabel.text = [NSString stringWithFormat:@"驾考类型：%@",_customerInfo.type];
    self.learnTimeLabel.text = @"学车时间：";
    if (_customerInfo.startTime!=nil&&_customerInfo.endTime!=nil)
    {
        NSString *date = [HDUtil formatter:@"M月d日" FromTime:[_customerInfo.startTime doubleValue]];
        NSString *startTime = [HDUtil formatter:@"HH:mm" FromTime:[_customerInfo.startTime doubleValue]];
        NSString *endTime = [HDUtil formatter:@"HH:mm" FromTime:[_customerInfo.endTime doubleValue]];
        self.learnTimeLabel.text = [NSString stringWithFormat:@"学车时间：%@ %@-%@ %@个小时",date,startTime,endTime,[HDUtil check:_customerInfo.learnTime]];
    }
    self.needCompanyBtn.hidden = !_customerInfo.needCompany;
    CGFloat remarkLabelWidth;
    if (_customerInfo.needCompany) {
        remarkLabelWidth = APPFRAME.size.width-160;
        self.priceLabel.left = self.needCompanyBtn.left-70;
    } else {
        remarkLabelWidth = APPFRAME.size.width-90;
        self.priceLabel.left = APPFRAME.size.width-70;
    }
    self.priceLabel.text = [NSString stringWithFormat:@"%@元",_customerInfo.price];
    self.remarkLabel.width = remarkLabelWidth;
    if (_customerInfo.orderType==HDOrderTypePractice) {
        self.remarkLabel.numberOfLines = 0;
        NSString *remarkStr = [NSString stringWithFormat:@"备注：%@",_customerInfo.remark];
        CGSize size = [HDUtil sizeWithString:remarkStr font:[UIFont systemFontOfSize:13] size:CGSizeMake(remarkLabelWidth, CGFLOAT_MAX)];
        self.remarkLabel.height = size.height>15?size.height:15;
        self.remarkLabel.text = remarkStr;
    } else {
        self.remarkLabel.numberOfLines = 1;
        self.remarkLabel.text = [NSString stringWithFormat:@"体验场：%@",_customerInfo.practicePosition];
    }
    NSString *orderStatusStr = _customerInfo.orderStatus;
    orderStatusStr = [orderStatusStr stringByReplacingOccurrencesOfString:@"已退单完成" withString:@"已取消"];
    self.statusTitleLabel.top = self.remarkLabel.bottom+15;
    self.orderStatusLabel.top = self.remarkLabel.bottom+15;
    self.line.top = self.remarkLabel.bottom+15;
    self.orderStatusLabel.text = orderStatusStr;
    self.subjectLabel.text = @"";
    if (_customerInfo.subject.length&&_customerInfo.orderType==HDOrderTypeLearn) {
        self.subjectLabel.text = _customerInfo.subject;
    }
}

- (void)callAction:(UIButton *)sender
{
    [HDUtil call:[HDUtil check:self.customerInfo.phoneNumber]];
}

#pragma mark - getters
- (DOImageView *)photo
{
    if (!_photo) {
        _photo = [[DOImageView alloc] initWithFrame:CGRectMake(10, 15, 50, 50)];
        _photo.layer.cornerRadius = 5;
        _photo.layer.masksToBounds = YES;
        _photo.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _photo;
}

- (UILabel *)nameLabel
{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.photo.right+15, 15, 100, 20)];
        _nameLabel.font = [UIFont systemFontOfSize:15];
        _nameLabel.textColor = kDarkGrayColor;
    }
    return _nameLabel;
}

- (UILabel *)stationTimeLabel
{
    if (!_stationTimeLabel) {
        _stationTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.photo.right+15, self.nameLabel.bottom, APPFRAME.size.width-130, 15)];
        _stationTimeLabel.font = [UIFont systemFontOfSize:12];
        _stationTimeLabel.textColor = [UIColor hexStringToColor:@"#424242"];
    }
    return _stationTimeLabel;
}

- (UILabel *)typeLabel
{
    if (!_typeLabel) {
        _typeLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.photo.right+15, self.stationTimeLabel.bottom, APPFRAME.size.width-130, 15)];
        _typeLabel.font = [UIFont systemFontOfSize:12];
        _typeLabel.textColor = kLightGrayColor;
    }
    return _typeLabel;
}

- (UILabel *)learnTimeLabel
{
    if (!_learnTimeLabel) {
        _learnTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 95, APPFRAME.size.width-20, 15)];
        _learnTimeLabel.font = [UIFont systemFontOfSize:13];
        _learnTimeLabel.textColor = kDarkGrayColor;
    }
    return _learnTimeLabel;
}

- (UILabel *)remarkLabel
{
    if (!_remarkLabel) {
        _remarkLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, self.learnTimeLabel.bottom+10, APPFRAME.size.width-95, 15)];
        _remarkLabel.font = [UIFont systemFontOfSize:13];
        _remarkLabel.textColor = kDarkGrayColor;
    }
    return _remarkLabel;
}

- (GXButton *)callBtn
{
    if (!_callBtn) {
        _callBtn = [GXButton buttonWithType:UIButtonTypeCustom];
        _callBtn.frame = CGRectMake(APPFRAME.size.width-50, 20, 40, 40);
        _callBtn.imageViewFrame = CGRectMake(2.5, 2.5, 35, 35);
        _callBtn.relayout = YES;
        [_callBtn setImage:[UIImage imageNamed:@"电话"] forState:UIControlStateNormal];
        [_callBtn addTarget:self action:@selector(callAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _callBtn;
}

- (GXButton *)needCompanyBtn
{
    if (!_needCompanyBtn) {
        _needCompanyBtn = [GXButton buttonWithType:UIButtonTypeCustom];
        _needCompanyBtn.frame = CGRectMake(APPFRAME.size.width-83, 115, 73, 25);
        _needCompanyBtn.clipsToBounds = YES;
        _needCompanyBtn.imageViewFrame = CGRectMake(2, 5, 19, 14);
        _needCompanyBtn.titleLabelFrame = CGRectMake(23, 0, 50, 25);
        _needCompanyBtn.relayout = YES;
        [_needCompanyBtn setTitle:@"需要接送" forState:UIControlStateNormal];
        [_needCompanyBtn setImage:[UIImage imageNamed:@"汽车.jpg"] forState:UIControlStateNormal];
        [_needCompanyBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _needCompanyBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        _needCompanyBtn.userInteractionEnabled = NO;
        _needCompanyBtn.hidden = YES;
    }
    return _needCompanyBtn;
}

- (UILabel *)priceLabel
{
    if (!_priceLabel) {
        _priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(APPFRAME.size.width-70, self.learnTimeLabel.bottom+10, 60, 15)];
        _priceLabel.textColor = [UIColor hexStringToColor:@"#ff2b01"];
        _priceLabel.textAlignment = NSTextAlignmentRight;
        _priceLabel.font = [UIFont systemFontOfSize:13];
    }
    return _priceLabel;
}

- (UILabel *)orderStatusLabel
{
    if (!_orderStatusLabel) {
        _orderStatusLabel = [[UILabel alloc] initWithFrame:CGRectMake(APPFRAME.size.width-70, self.remarkLabel.bottom+15, 50, 44)];
        _orderStatusLabel.font = [UIFont systemFontOfSize:13];
        _orderStatusLabel.textColor = [UIColor hexStringToColor:@"#e94929"];
    }
    return _orderStatusLabel;
}

- (UILabel *)subjectLabel
{
    if (!_subjectLabel) {
        _subjectLabel = [[UILabel alloc] initWithFrame:CGRectMake(APPFRAME.size.width-115, 15, 50, 20)];
        _subjectLabel.font = [UIFont systemFontOfSize:14];
        _subjectLabel.textColor = kDarkGrayColor;
    }
    return _subjectLabel;
}

@end
